package com.gxg.demo10.mydemo11.huaban;

import android.Manifest;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.gxg.demo10.mydemo11.R;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.io.File;
import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.forward.androids.utils.LogUtil;
import cn.hzw.doodle.DoodleActivity;
import cn.hzw.doodle.DoodleParams;
import cn.hzw.doodle.DoodleView;
import cn.hzw.imageselector.ImageLoader;
import cn.hzw.imageselector.ImageSelectorActivity;
import io.reactivex.functions.Consumer;

public class HuaBanActivity extends AppCompatActivity {
    @Bind(R.id.iv_back)
    ImageView ivBack;
    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.ll_left_all)
    LinearLayout llLeftAll;
    @Bind(R.id.tv_name)
    TextView tvName;
    @Bind(R.id.ll_logout)
    LinearLayout llLogout;
    @Bind(R.id.ll_search)
    LinearLayout llSearch;
    @Bind(R.id.ll_left)
    LinearLayout llLeft;
    @Bind(R.id.iv_right)
    ImageView ivRight;
    @Bind(R.id.ll_right)
    LinearLayout llRight;
    @Bind(R.id.ll_right_all)
    LinearLayout llRightAll;
    @Bind(R.id.base_toolbar)
    RelativeLayout baseToolbar;
    @Bind(R.id.btn_img)
    Button btnImg;
    @Bind(R.id.btn_white)
    Button btnWhite;
    @Bind(R.id.iv_img)
    ImageView ivImg;
    @Bind(R.id.img_path)
    TextView imgPath;
    private RxPermissions rxPermissions;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_huaban);
        ButterKnife.bind(this);

        rxPermissions = new RxPermissions(this);

        rxPermissions.request(Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE)
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        if(aBoolean){
                            LogUtil.e("sss  同意权限");
                        }else{
                            LogUtil.e("sss  不同意权限");
                        }
                    }
                });
    }

    public static final int REQ_CODE_SELECT_IMAGE = 100;
    public static final int REQ_CODE_DOODLE = 101;

    @OnClick({R.id.ll_left_all, R.id.btn_img, R.id.btn_white})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_left_all:
                break;
            case R.id.btn_img:
                ImageSelectorActivity.startActivityForResult(REQ_CODE_SELECT_IMAGE, HuaBanActivity.this, null, false);
                break;
            case R.id.btn_white:
                LogUtil.e("sss   "+Environment.getExternalStorageDirectory().getAbsolutePath());
                AssetsToSDCardUtils.getInstance(HuaBanActivity.this).copyAssetsToSD2("baizhi2.jpg").setFileOperateCallback(new AssetsToSDCardUtils.FileOperateCallback() {
                    @Override
                    public void onSuccess() {
                        LogUtil.e("sss  成功");
                        // 涂鸦参数
                        DoodleParams params = new DoodleParams();
                        params.mIsFullScreen = true;
                        // 图片路径
                        String absolutePath = new File(getCacheDir(), "baizhi2.jpg").getAbsolutePath();
                        LogUtil.e("sss   "+absolutePath);
                        params.mImagePath =absolutePath;
                        // 初始画笔大小
                        params.mPaintUnitSize = DoodleView.DEFAULT_SIZE;
                        // 画笔颜色
                        params.mPaintColor = Color.RED;
                        // 是否支持缩放item
                        params.mSupportScaleItem = true;
                        // 启动涂鸦页面
                        DoodleActivity.startActivityForResult(HuaBanActivity.this, params, REQ_CODE_DOODLE);
                    }

                    @Override
                    public void onFailed(String error) {
                        LogUtil.e("sss  失败"+error);
                    }
                });

                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQ_CODE_SELECT_IMAGE) {
            if (data == null) {
                return;
            }
            ArrayList<String> list = data.getStringArrayListExtra(ImageSelectorActivity.KEY_PATH_LIST);
            if (list != null && list.size() > 0) {
                LogUtil.d("Doodle", list.get(0));

                // 涂鸦参数
                DoodleParams params = new DoodleParams();
                params.mIsFullScreen = true;
                // 图片路径
                params.mImagePath = list.get(0);
                // 初始画笔大小
                params.mPaintUnitSize = DoodleView.DEFAULT_SIZE;
                // 画笔颜色
                params.mPaintColor = Color.RED;
                // 是否支持缩放item
                params.mSupportScaleItem = true;
                // 启动涂鸦页面
                DoodleActivity.startActivityForResult(HuaBanActivity.this, params, REQ_CODE_DOODLE);
            }
        } else if (requestCode == REQ_CODE_DOODLE) {
            if (data == null) {
                return;
            }
            if (resultCode == DoodleActivity.RESULT_OK) {
                String path = data.getStringExtra(DoodleActivity.KEY_IMAGE_PATH);
                if (TextUtils.isEmpty(path)) {
                    return;
                }
                ImageLoader.getInstance(this).display(findViewById(R.id.iv_img), path);
                imgPath.setText(path);
            } else if (resultCode == DoodleActivity.RESULT_ERROR) {
                Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_SHORT).show();
            }
        }
    }
}
